/**
 * 自定义函数
 * */


var formList = {
    data: [],
    pages: {start: 0, limit: 10}
}; //场景数据列表
var formArys = {
    name: '', //场景名称
    view: '' //页面名称
}; //表单数据


//场景列表
function getIndex(){
    var formData = {
        aid: getAid(),
        pageStart: formList.pages.start,
        pageLimit: formList.pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.scenceIndex.method,
        url: domain + apis.scenceIndex.url,
        data: formData,
        // headers: {
        //     aid: adminInfo.aid
        // },
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                if (formList.pages.start > 0) {
                    msgPop({msg: res.msg});
                }
                return
            }
            for (var i = 0; i < res.data.length; i ++) {
                formList.data.push(res.data[i]);
            }
            formList.pages.start ++;
            getHtml();
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//场景列表字符串
function getHtml(){
    var h = '', i, d = formList.data;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].name + '</td>';
        h += '<td>' + d[i].tmp_name + '</td>';
        h += '<td>' + d[i].view + '</td>';
        h += '<td>';
        h += '<a href="' + routes.videoIndex + '?scenceId=' + d[i].id + '&scenceTitle=' + escape(d[i].name) + '"title="视频" class="ml-5" style="color: #0088cc;"><b>' + d[i].videoNum + ' 个</b></a>';
        h += '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" title="编辑" class="ml-5" onclick="viewEdit(' + d[i].id + ');">';
        h += ' 编辑<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        //h += ' &nbsp; ';
        //h += '<a href="' + routes.svIndex + '?scenceId=' + d[i].id + '&scenceName=' + escape(d[i].name) + '" title="视频" class="ml-5">';
        //h += ' 视频';
        //h += '</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" title="删除" class="ml-5" style="color: red;" onclick="viewDel(' + d[i].id + ');">';
        h += ' 删除';
        h += '</a>';
        h += '</td>';
        h += '</tr>';
    }
    h += '';
    $('#formList').html(h);
    $('#tableMore').show();
}


//表单新增
function viewAdd(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">新增</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //场景名
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>场景名称：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入场景名称" value="" onblur="formArys.name=this.value;"/>';
    formArys.placeName = 'H5广告位';
    h += '</div>';
    h += '</div>';
    //页面名称
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>页面名称：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入页面名称" value="" onblur="formArys.view=this.value;"/>';
    formArys.total = 5;
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    //h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="addOne();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//场景新增
function addOne(){
    if (formArys.name == '') {
        msgPop({msg: '场景名称必须的'}); return
    }
    if (formArys.view == '') {
        msgPop({msg: '页面名称必须的'}); return
    }
    var formData = {
        aid: getAid(),
        name: formArys.name,
        view: formArys.view
    };
    console.log(formData);
    $.ajax({
        type: apis.scenceAdd.method,
        url: domain + apis.scenceAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '添加成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//表单修改
function viewEdit(id){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var i, scenceOne = '';
    for (i = 0; i < formList.data.length; i ++) {
        if (formList.data[i].id == id) {
            scenceOne = formList.data[i];
        }
    }
    if (scenceOne == '') {
        msgPop({msg: '场景不存在'}); return
    }
    console.log(scenceOne);
    var h = '';
    h += '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">修改</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //场景名
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>场景名称：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入场景名称" value="' + scenceOne.name + '" onblur="formArys.name=this.value;"/>';
    h += '<input type="hidden" id="scenceId" value="' + scenceOne.id + '"/>';
    formArys.name = scenceOne.name;
    h += '</div>';
    h += '</div>';
    //页面名称
    h += '<div class="row cl">';
    h += '<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>页面名称：</label>';
    h += '<div class="formControls col-xs-8 col-sm-9">';
    h += '<input type="text" class="input-text" placeholder="输入页面名称" value="' + scenceOne.view + '" onblur="formArys.view=this.value;"/>';
    formArys.view = scenceOne.view;
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    //h += '<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">';
    h += '<div class="col-xs-8 col-sm-9 col-xs-offset-3 col-sm-offset-2">';
    h += '<button class="btn btn-default radius" type="button" onClick="$(\'#formPop\').html(\'\');">&nbsp; 取 消 &nbsp;</button> <button class="btn btn-secondary radius" type="button" onClick="updateOne();"><i class="Hui-iconfont">&#xe632;</i> 保 存 </button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//场景修改
function updateOne(){
    var id = $('#scenceId').val();
    if (formArys.name == '') {
        msgPop({msg: '场景名称必须的'}); return
    }
    if (formArys.view == '') {
        msgPop({msg: '页面名称必须的'}); return
    }
    var formData = {
        aid: getAid(),
        id: id,
        name: formArys.name,
        view: formArys.view
    };
    console.log(formData);
    $.ajax({
        type: apis.scenceModify.method,
        url: domain + apis.scenceModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '修改成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function viewDel(id){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var i, scenceOne = '';
    for (i = 0; i < formList.data.length; i ++) {
        if (formList.data[i].id == id) {
            scenceOne = formList.data[i];
        }
    }
    if (scenceOne == '') {
        msgPop({msg: '场景不存在'}); return
    }
    console.log(scenceOne);
    var h = '';
    h += '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.6) + 'px; text-align: center; background: #fff; color: #333; border-radius: 5px; position: fixed; top: 5%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">';
    h += '<div style="text-align: center; font-weight: bolder; font-size: 20px;">删除</div>';
    h += '<form class="form form-horizontal" style="padding: 20px; max-height: 500px; overflow-x: hidden; overflow-y: auto;">';
    //确认
    h += '<div class="row cl">';
    h += '<div class="formControls" style="margin: 0 auto; text-align: center;">';
    h += '确认要 <b style="color: red; font-size: 30px;">删除</b> 嘛？';
    h += '<input type="hidden" id="scenceId" value="' + scenceOne.id + '"/>';
    h += '</div>';
    h += '</div>';
    //按钮
    h += '<div class="row cl">';
    h += '<div class="col-xs-8 col-sm-12">';
    h += '<button class="btn btn-default radius" type="button" style="width: 100px;" onClick="$(\'#formPop\').html(\'\');">取消</button> &nbsp; <button class="btn btn-secondary radius" type="button" style="width: 100px; background: red;;" onClick="delOne();">删除</button>';
    h += '</div>';
    h += '</div>';
    h += '</form>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('#formPop').html(h);
}


//场景删除
function delOne(){
    var id = $('#scenceId').val();
    var formData = {
        aid: getAid(),
        id: id
    };
    console.log(formData);
    $.ajax({
        type: apis.scenceDel.method,
        url: domain + apis.scenceDel.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '删除成功'});
            window.location.href = '';
        },
        error: function () {
            console.log('网络错误');
        }
    });
}